package cn.wizzer.app.web.modules.controllers.crown.order;

import cn.wizzer.app.crown.modules.models.vo.OrderParamVO;
import cn.wizzer.app.crown.modules.services.order.CrownOrderService;
import cn.wizzer.app.sys.modules.services.SysParamService;
import cn.wizzer.app.user.modules.services.UserRestrictionService;
import cn.wizzer.app.web.commons.filter.ApiTokenFilter;
import cn.wizzer.app.web.commons.ulog.annotation.ULog;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.ApiParam;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.*;

import java.math.BigDecimal;

/**
 * 游戏大厅注单、记录Controller
 * @author
 */
@At("/api/match/hg/order")
@IocBean
public class ApiCrownOrderController {
    @Reference(retries = 0, timeout = 5000)
    @Inject
    private CrownOrderService orderService;
    @Inject
    private RedisService redisService;
    @Reference
    @Inject
    private SysParamService sysParamService;
    @Reference
    @Inject
    private UserRestrictionService restrictionService;

    /**
     * 冠军时传champion
     * 单式 simplex
     * 综合 传simplex
     *
     * @return
     */
    @At("/create")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    @AdaptBy(type = JsonAdaptor.class)
    @ULog(tag = "用户注单创建", msg = "注单金额: ${vo.amount}, 单式: ${vo.simplex}, 综合: ${vo.simplexes}, 冠军: ${vo.champion}")
    public Result create(@ApiParam @Param("..") OrderParamVO vo) {
        if (!sysParamService.fetch().getGameSwitch()) {
            // 前端大佬要code=0，data为false。
            return Result.success(false);
        }
        String appid = Mvcs.getReq().getHeader("appid");
        if (null == vo) {
            return Result.error();
        }
        // 判断金额小于等于0
        if (vo.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
            return Result.error("金额有误");
        }
        if (vo.getChampion() != null) {
            orderService.createChampion(appid, vo.getAmount(), vo.getChampion());
        } else if (vo.getSimplex() != null) {
            orderService.createSimplex(appid, vo.getAmount(), vo.getSimplex());
        } else if (vo.getSimplexes() != null) {
            orderService.createComplex(appid, vo.getAmount(), vo.getSimplexes());
        } else {
            return Result.error("没有玩法");
        }
        return Result.success();
    }


    /**
     * 投注记录
     *
     * @param pageNo   页码
     * @param pageSize 容量
     * @return Result
     */
    @At("/list")
    @Ok("json:{locked:'gameId|no1|no2|no3|matchHalfScore|matchStage',ignoreNull:true}")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result list(@Param("orderType") String orderType,
                       @Param("matchType") String matchType,
                       @Param("playWay") String playWay,
                       @Param("startTime") Long startTime,
                       @Param("endTime") Long endTime,
                       @Param("pageNo") int pageNo,
                       @Param("pageSize") int pageSize) {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success(orderService.list(appid, orderType, matchType, playWay, startTime, endTime, pageNo, pageSize));
    }

    /**
     * 投注详情
     *
     * @param orderId 订单ID
     * @return Result
     */
    @At("/detail")
    @Ok("json:{locked:'no1|no2|no3',ignoreNull:true}")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result detail(@Param("orderId") String orderId) {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success(orderService.detail(appid, orderId));
    }

    /**
     * 用户限额
     * @return Result
     */
    @At("/userRestriction")
    @Ok("json:{locked:'dayRestriction|totalRestriction|selfRate|superRate|giftRate|viewRate',ignoreNull:true}")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result userRestriction(){
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success(restrictionService.initCache(appid));
    }

}





